﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TorchView.GameContent.Data;

namespace TorchView.GameContent.Units
{
  public sealed class UnitSkill : IDataSerializable
  {
    public const string BLOCK_NAME = "SKILL";

    #region IDataSerializable Members

    public DataBlock MakeDataBlock()
    {
      var block = Data == null ? new DataBlock(BLOCK_NAME) : Data;
      block.SetOrClearPropertyValue(Name != "", "NAME", Name);
      block.SetOrClearPropertyValue(Enabled, "ENABLED", Enabled);
      block.SetPropertyValue("LEVEL", Level);
      block.SetOrClearPropertyValue(LevelRequired >= 1, "LEVEL_REQUIRED", LevelRequired);
      block.SetOrClearPropertyValue(!string.IsNullOrEmpty(SkillRequired), "SKILL_REQUIRED", SkillRequired);
      block.SetOrClearPropertyValue(!string.IsNullOrEmpty(AnimationOverride), "ANIMATION_OVERRIDE", AnimationOverride);
      block.SetOrClearPropertyValue(!string.IsNullOrEmpty(AnimationOverrideDW), "ANIMATION_OVERRIDEDW", AnimationOverrideDW);
      block.SetOrClearPropertyValue(!string.IsNullOrEmpty(AnimationOverrideLoop), "ANIMATION_OVERRIDELOOP", AnimationOverrideLoop);
      block.SetOrClearPropertyValue(!string.IsNullOrEmpty(AnimationOverrideLoopDW), "ANIMATION_OVERRIDELOOPDW", AnimationOverrideLoopDW);
      block.SetOrClearPropertyValue(Chance != 40, "CHANCE", Chance);
      block.SetOrClearPropertyValue(CancelChance != 0, "CANCEL_CHANCE", CancelChance);
      block.SetPropertyValue("PANE", Pane);
      block.SetPropertyValue("COLUMN", Column);
      block.SetPropertyValue("ROW", Row);
      return block;
    }

    public void LoadFromDataBlock(DataBlock block)
    {
      if (block == null)
        throw new ArgumentNullException("block");
      if (block.Name != BLOCK_NAME)
        throw new ArgumentException("Incorrect block type, only " + BLOCK_NAME + " is supported.");

      Data = block;
      Name = block.GetPropertyValue("NAME", "");
      Enabled = block.GetPropertyValue("ENABLED", true);
      Level = block.GetPropertyValue("LEVEL", -1);
      Chance = block.GetPropertyValue("CHANCE", 40);
      CancelChance = block.GetPropertyValue("CANCEL_CHANCE", 0);
      LevelRequired = block.GetPropertyValue("LEVEL_REQUIRED", -1);
      SkillRequired = block.GetPropertyValue("SKILL_REQUIRED", "");
      AnimationOverride = block.GetPropertyValue("ANIMATION_OVERRIDE", "");
      AnimationOverrideDW = block.GetPropertyValue("ANIMATION_OVERRIDEDW", "");
      AnimationOverrideLoop = block.GetPropertyValue("ANIMATION_OVERRIDELOOP", "");
      AnimationOverrideLoopDW = block.GetPropertyValue("ANIMATION_OVERRIDELOOPDW", "");
      Pane = block.GetPropertyValue("PANE", 0);
      Column = block.GetPropertyValue("COLUMN", -1);
      Row = block.GetPropertyValue("ROW", -1);
    }

    #endregion

    public DataBlock Data { get; set; }
    public string Name { get; set; }
    public bool Enabled { get; set; }
    public int Level { get; set; }    
    public int Chance { get; set; }
    public int CancelChance { get; set; }
    public int LevelRequired { get; set; }
    public string SkillRequired { get; set; }
    public string AnimationOverride { get; set; }
    public string AnimationOverrideDW { get; set; }
    public string AnimationOverrideLoop { get; set; }
    public string AnimationOverrideLoopDW { get; set; }
    public int Pane { get; set; }
    public int Column { get; set; }
    public int Row { get; set; }
  }
}
